<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      ref="queryFormRef"
      :inline="true"
      :model="queryParams"
      class="-mb-15px"
      label-width="100px"
    >
      <el-form-item label="模板名称" prop="templatename">
        <el-input v-model="queryParams.templatename" placeholder="请输入模板名称" />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon class="mr-5px" icon="ep:search" />
          搜索
        </el-button>
        <el-button plain type="primary" @click="openForm('create')">
          <Icon class="mr-5px" icon="ep:plus" />
          新增
        </el-button>
        <el-button @click="resetQuery">
          <Icon class="mr-5px" icon="ep:refresh" />
          重置
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <PureTableBar title="" :columns="columns" @refresh="handleQuery">
      <template #default="{ size, dynamicColumns }">
        <pure-table
          ref="tableRef"
          row-key="id"
          align-whole="center"
          :loading="loading"
          :size="size"
          :data="list"
          :columns="dynamicColumns"
        >
          <template #operation="scope">
            <div class="flex items-center justify-center">
              <el-button link type="primary" @click="openForm('update', scope.row.id)"
                >编辑</el-button
              >
            </div>
          </template>
          <template #empty>
            <Empty />
          </template>
        </pure-table>
        <Pagination
          v-model:limit="queryParams.pageSize"
          v-model:page="queryParams.pageNo"
          :total="total"
          @pagination="getList"
        />
      </template>
    </PureTableBar>
  </ContentWrap>
</template>

<script lang="ts" setup>
import * as FormApi from '@/api/mall/tuina/form/index'

import { PureTableBar } from '@/components/RePureTableBar'

defineOptions({ name: 'FormDesignerList' })

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  templatename: null
})
const queryFormRef = ref() // 搜索的表单
const tableRef = ref()
import { useTable } from './utils/hook'
const { columns } = useTable()
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await FormApi.getFormPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const { push } = useRouter() // 路由跳转

const openForm = (type: string, id?: number) => {
  if (type == 'update') {
    push({ name: 'formDesignerEdit', params: { id } })
    return
  }
  push({ name: 'formDesignerAdd' })
}

/** 初始化 **/
onActivated(() => {
  getList()
})
onMounted(() => {
  getList()
})
</script>
